﻿using System.Linq;
using Halfwit.Pollers;
using System.Windows.Threading;
using System;
using System.Windows.Input;
using TweetSharp.Twitter.Extensions;
using TweetSharp.Twitter.Fluent;
using TweetSharp.Twitter.Model;

namespace Halfwit.ViewModels
{
    public class UserViewModel : TweetsViewModel
    {
        //public UserViewModel(MainViewModel host, string user)
        //    : base(host)
        //{
        //    var request = TwitterConnection.CreateRequest()
        //        .Users().ShowProfileFor(user);

        //    var result = request.Request();
        //    if (result.IsFailWhale || result.IsNetworkError || result.IsTwitterError)
        //    {
        //        return;
        //    }

        //    User = result.AsUser();

        //    Search = new RelayCommand("Search", PerformSearch, CanPerformSearch);

        //    Pollers.Add(new UserPoller(() => this.User.ScreenName, PollCompleted));
        //}

        public UserViewModel(MainViewModel host, TwitterUser user)
            : base(host)
        {
            User = user;
            Search = new RelayCommand("Search", PerformSearch, CanPerformSearch);

            Pollers.Add(new UserPoller(() => this.User.ScreenName, PollCompleted));
        }

        private string _text;
        private TwitterUser _user;

        private bool CanPerformSearch(object o)
        {
            return !String.IsNullOrEmpty(_text.Trim());
        }

        private void PerformSearch(object o)
        {
            if (_text != User.ScreenName)
            {
                var request = TwitterConnection.CreateRequest()
                    .Users().ShowProfileFor(_text);

                var result = request.Request();
                if (result.IsFailWhale || result.IsNetworkError || result.IsTwitterError)
                {
                    var error = result.AsError();

                    Status = error != null ? error.ErrorMessage
                        : !String.IsNullOrEmpty(result.ResponseHttpStatusDescription) ? result.ResponseHttpStatusDescription
                        : "Unknown error. Possible space-time continuum anomaly.";
                    return;
                }

                var user = result.AsUser();
                if (user == null)
                {
                    Status = "User not found";
                    return;
                }

                User = user;
            }

            Tweets.Clear();
            Pollers[0].Reset();
            Refresh.Execute(null);
        }

        public ICommand Search { get; private set; }

        public string Text
        {
            get { return _text; }
            set
            {
                _text = value;
                OnPropertyChanged("Text");
            }
        }

        public TwitterUser User
        {
            get { return _user; }
            set
            {
                _user = value;
                OnPropertyChanged("User");

                Title = "Tweets by " + _user.Name;
                OnPropertyChanged("Title");

                Uri = "http://twitter.com/" + User.ScreenName;
                OnPropertyChanged("Uri");

                _text = User.ScreenName;
            }
        }
    }
}
